在谷歌插件中使用nodejs模块【JIE's Blos】 您所在的位置:网站首页 nodejs 库 在谷歌插件中使用nodejs模块【JIE's Blos】

在谷歌插件中使用nodejs模块【JIE's Blos】

2023-03-11 22:10| 来源: 网络整理| 查看: 265

-

相关

这次要介绍如何在谷歌插件中使用nodejs模块,演示的项目是 一个后台定时提醒的插件

运行环境 nodejs Windows、Linux 或 MacOS 操作系统 chrome浏览器 browserify

##基础框架搭建

###创建一个基础的chrome插件框架可以从谷歌插件的github克隆相应的例子进行修改

基本架构只需要包含:

manifest.json (谷歌插件配置文件) background.js (后台逻辑js文件)

##将nodejs库使用转化为浏览器可使用的库

###下载安装browserifynpm install browserify -g

###下载node-schedule库

cd /mnt && npm install node-schedule cd node_modules browserify -r node-schedule/:node-schedule > node-schedule.js

重点:browserify -r node-schedule/:node-schedule > node-schedule.js命令中的node-schedule/:node-schedule中:后面指定browserify转换后生成的js文件可被require的名字require(node-schedule)

将生成的node-schedule.js文件复制到项目里面

##编写逻辑代码

###background.js123456789101112131415161718192021222324252627var scheduler = require("node-schedule")Schedule();function Schedule() { //每5秒弹出test的通知 scheduler.scheduleJob('*/5 * * * * *', function () { notifyMe("test"); });}function notifyMe(msg, url) { if (!Notification) { alert('Desktop notifications not available in your browser. Try Chromium.'); return; } if (Notification.permission !== "granted") Notification.requestPermission(); else { var notification = new Notification('test', { body: msg, }); };}

manifest.json123456789101112131415{ "manifest_version": 2, "name": "googole-extension-test", "version": "1.0", "description": "googole-extension-test", "permissions": [ "notifications" ], "background": { "scripts": [ "node-schedule.js", "background.js" ] }} 注意事项

manifest.json中background - >scripts顺序必须是node-schedule.js在background.js前面才能够将node-schedule.js文件中的require函数被background.js识别到(和页面js加载相同原理)

##加载运行百度

##demo地址

推荐文章如何使用visual stdio code编写TypeScriptnodejs单元测试pomelo启用第三方rpc库---pomelo-rpc-ws


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有